package com.example.todolist.service;

import com.example.todolist.entity.ReviewTask;
import com.example.todolist.entity.Summary;
import com.example.todolist.entity.Task;
import com.example.todolist.entity.User;
import com.example.todolist.mapper.*;
import com.example.todolist.tools.ResponseBody;
import com.example.todolist.tools.StringToDateConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
public class SummaryService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private ReviewTaskMapper reviewTaskMapper;
    @Autowired
    private HabitMapper habitMapper;
    @Autowired
    private HabitService habitService;
    public StringToDateConverter stringToDateConverter = new StringToDateConverter();

    public ResponseBody<List<Task>> getThisMonthTask(Integer userId, Integer year, Integer month) {
        User user = userMapper.findByUserId(userId);
        if (user == null) {
            return new ResponseBody<>(-1, "userId error", null);
        }
        List<Task> tasks = taskMapper.findMonthTask(userId, year, month);
        return new ResponseBody<>(200, "success", tasks);
    }

    public ResponseBody<List<ReviewTask>> getThisMonthReviewTask(Integer userId,Integer year,Integer month) {
        return new ResponseBody<>(200,"success",reviewTaskMapper.getThisMonthReviewTask(userId,year,month));
    }

    public ResponseBody<List<Task>> getOneDayTask(Integer userId, Date date) {
        User user = userMapper.findByUserId(userId);
        if (user == null) {
            return new ResponseBody<>(-1, "this userId does not exist", null);
        }
        return new ResponseBody<>(200, "success", taskMapper.getOneDayTask(userId, date));
    }

    public ResponseBody<List<ReviewTask>> getOneDayReviewTask(Integer userId, Date date) {
        return new ResponseBody<>(200,"success",reviewTaskMapper.getOneDayTask(userId,date));
    }

    public ResponseBody<Summary> getSummary(Integer userId) {
        List<Task> tasks = taskMapper.findByUserId(userId);
        int taskTotal = tasks.size();
        int taskDone = taskMapper.countTheTaskDone(userId) ;
        Date date = Date.valueOf(LocalDate.now());
        int reviewTaskTotal = reviewTaskMapper.getOneDayTask(userId,date).size();
        int reviewTaskDone = reviewTaskMapper.countTheTaskDone(userId,date);
        Summary summary = new Summary();
        summary.setTaskTotal(taskTotal);
        summary.setTaskDone(taskDone);
        summary.setReviewTaskTotal(reviewTaskTotal);
        summary.setReviewTaskDone(reviewTaskDone);
        return new ResponseBody<>(200, "success", summary);
    }

    public ResponseBody<List<String>> getInformationToGPT(Integer userId) {
        User user=userMapper.findByUserId(userId);
        if(user==null)
        {
            return new ResponseBody<>(-1,"userId does not exist",null);
        }

        List<String> taskp = taskMapper.getLastDayTask(userId);
        List<String> tasks = new ArrayList<>();
        String taskDescription = "finish a task ";
        for(String s : taskp){
            tasks.add(taskDescription+s);
        }

        List<String> reviewTaskp = reviewTaskMapper.getLastDayReviewTask(userId);
        List<String> reviewTasks = new ArrayList<>();
        String reviewTaskDescription = "review the ";
        for(String s : reviewTaskp){
            reviewTasks.add(reviewTaskDescription+s);
        }

        Date today = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String yesterday = simpleDateFormat.format(today);
        Date date = stringToDateConverter.StringToDate(yesterday);
        String weekDay = habitService.getWeekDay(date);
        List<String> habits = habitMapper.getLastDayHabit(userId,weekDay);
        List<String> habit = new ArrayList<>();
        String habitsDescription = "keep a habits ";
        for (String s : habits) {
            habit.add(habitsDescription + s);
        }

        tasks.addAll(reviewTasks);
        tasks.addAll(habit);
        return new ResponseBody<>(200,"success",tasks);
    }

    public ResponseBody<List<ReviewTask>> getForgetReviewTask(Integer userId) {
        Date date = Date.valueOf(LocalDate.now());
        return new ResponseBody<>(200,"success",reviewTaskMapper.getForgetReviewTask(userId,date));
    }
}
